<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------
namespace api\portal\service;

use app\portal\model\House;
use app\portal\model\HouseFieldType;
use think\Db;

class HouseService
{

  static public function listData($params = []) {
    $pagesize = 20;

    $lastId = 0;
    if (isset($params['lastid']) && intval($params['lastid']) > 0) {
      $lastId = $params['lastid'];
    }

    if (isset($params['sell_type']) && intval($params['sell_type']) > 0 && intval($params['sell_type']) <= 4) {
      return self::oneTypeList($params, $params['sell_type'], $pagesize, $lastId);
    }

    $lastSellType = 1;
    if (isset($params['last_sell_type']) && intval($params['last_sell_type']) > 0 && intval($params['last_sell_type']) <= 4) {
      $lastSellType = $params['last_sell_type'];
    }

    $list = [];
    $count = 0;
    for ($i = $lastSellType; $i <= 4; $i++) {
      $num = $pagesize - count($list);
      $startId = 0;
      if ($i == $lastSellType) {
        $startId = $lastId;
      }
      $typeList = self::oneTypeList($params, $i, $num, $startId);
      foreach ($typeList as $key => $value) {
        $list[] = $value;
        $lastId = $value['id'];
      }

      $count = count($list);
      if ($count == $pagesize) {
        break;
      }
    }

    return $list;
  }

  // oneTypeList 获取某个类型的房源列表
  static function oneTypeList($params = [], $type, $num, $startId = false) {
    $list = House::with('count')->orderRaw('id desc')->limit($num);

    if (isset($params['house_type']) && $params['house_type'] != '') {
      $list->where('myself', $params['house_type']);
    }

    // 面积
    if (isset($params['area']) && !empty($params['area'])) {
      self::areaFilter($list, $params['area']);
    }

    // 区域删选
    if (isset($params['region']) && !empty($params['region'])) {
      $list->where('region', $params['region']);
    }

    // 户型筛选
    if (isset($params['structure_type']) && !empty($params['structure_type'])) {
      $list->where('structure_type', $params['structure_type']);
    }

    // 朝向筛选
    if (isset($params['direction']) && !empty($params['direction'])) {
      $list->where('direction', $params['direction']);
    }

    // 装修类型筛选
    if (isset($params['decoration_type']) && !empty($params['decoration_type'])) {
      $list->where('decoration_type', $params['decoration_type']);
    }

    // 价格筛选
    if (isset($params['price']) && !empty($params['price'])) {
      self::priceFilter($list, $params['price']);
    }

    // 楼层筛选
    if (isset($params['floor']) && !empty($params['floor'])) {
      self::floorFilter($list, $params['floor']);
    }

    // 出售状态筛选
    $list->where('sell_type', $type);

    // 搜索
    if (isset($params['search']) && !empty($params['search'])) {
      self::search($list, $params['search']);
    }

    // 分页
    if ($startId) {
      $list->where('id', '<', $startId);
    }

    $list = $list->select();

    // 获取参数
    $fieldIds = [];
    foreach ($list as $key => $value) {
      if (!isset($fieldIds[$value->structure_type])) {
        $fieldIds[$value->structure_type] = $value->structure_type;
      }

      if (!isset($fieldIds[$value->decoration_type])) {
        $fieldIds[$value->decoration_type] = $value->decoration_type;
      }

      if (!isset($fieldIds[$value->direction])) {
        $fieldIds[$value->direction] = $value->direction;
      }
    }

    $paramsData = HouseFieldType::whereIn('id', $fieldIds)->select();
    $params = [];
    foreach ($paramsData as $key => $value) {
      $params[$value->id] = $value->name;
    }

    foreach ($list as $key => $value) {
      $list[$key]['structure_type'] = $params[$value['structure_type']];
      $list[$key]['decoration_type'] = $params[$value['decoration_type']];
      $list[$key]['direction'] = $params[$value['direction']];
    }

    return $list;
  }

  static private function priceFilter($list, $value) {
    $values = explode('-', $value);
    if (count($values) == 2) {
      $method = $values[0];
      $value = $values[1];

      if ($method == 'lower') {
        $list->where('price', '<=', $value);
      } elseif ($method == 'higher') {
        $list->where('price', '>=', $value);
      } else {
        if (intval($method) != 0) {
          $list->where('price', '>=', $method)->where('price', '<=', $value);
        }
      }
    }
  }

  static private function search($list, $keyword) {
    $list->where('title', 'like', "%{$keyword}%")
         ->whereor('keywords', 'like', "%{$keyword}%");
  }

  static private function floorFilter ($list, $floor) {
    if ($floor == 'top') {
      $list->where('floor=top_floor');
    } elseif ($floor == 'bottom') {
      $list->where('floor', 1);

    } elseif($floor == 'center') {
      $list->where("floor > 1")->where("floor < top_floor");
    } else {
      $values = explode('-', $floor);

      if (count($values) == 2) {
        $min = $values[0];
        $max = $values[1];
        if (intval($min) > 0 && intval($max) > 0) {
          $list->where("floor >= {$min}")->where("floor <= {$max}");
        }
      }
    }
  }

  // 面积筛选
  static public function areaFilter($list, $value) {
    $values = explode('-', $value);
    if (count($values) == 2) {
      $method = $values[0];
      $value = $values[1];

      if ($method == 'lower') {
        $list->where('acreage', '<=', $value);
      } elseif ($method == 'higher') {
        $list->where('acreage', '>=', $value);
      } else {
        if (intval($method) != 0) {
          $list->where('acreage', '>=', $method)->where('acreage', '<=', $value);
        }
      }
    }
  }

  static public function selParams() {
    // 获取参数
    $fieldIds = [];
    foreach ($list as $key => $value) {
      if (!isset($fieldIds[$value->structure_type])) {
        $fieldIds[$value->structure_type] = $value->structure_type;
      }

      if (!isset($fieldIds[$value->decoration_type])) {
        $fieldIds[$value->decoration_type] = $value->decoration_type;
      }

      if (!isset($fieldIds[$value->direction])) {
        $fieldIds[$value->direction] = $value->direction;
      }
    }

    $paramsData = HouseFieldType::whereIn('id', $fieldIds)->select();
    $params = [];
    foreach ($paramsData as $key => $value) {
      $params[$value->id] = $value->name;
    }
  }

  // 统计增加
  static public function add_count($id, $type, $method = 'add') {
    $get = Db::name('house_counts')->where('house_id', $id)->find();
    $res = false;
    if (!empty($get)) {
      $data = [];
      if ($method == 'add') {
        $data[$type] = $get[$type] + 1;
      } elseif ($method == 'reduce') {
        $data[$type] = $get[$type] - 1;
      }
      if ($data[$type] < 0) {
        $data[$type] = 0;
      }
      $res = Db::name('house_counts')->where('house_id', $id)->update($data);
    } else {
      $data = [];
      $data['house_id'] = $id;
      $data[$type] = 1;
      $res = Db::name('house_counts')->insert($data);
    }

    return $res;
  }

}
